package com.xlh.controller.courseTest;

import com.xlh.aop.PreAcquireLock;
import com.xlh.common.Result;
import com.xlh.constant.ReportConstant;
import com.xlh.dto.PageDTO;
import com.xlh.dto.courseTest.check.CheckDTO;
import com.xlh.dto.courseTest.check.ClassDTO;
import com.xlh.dto.courseTest.check.StudentListDTO;
import com.xlh.exception.common.GlobalException;
import com.xlh.param.check.CheckClassParam;
import com.xlh.param.check.CheckFullMarkParam;
import com.xlh.param.check.CheckSubmitParam;
import com.xlh.param.courseTest.DuplicateParam;
import com.xlh.pojo.courseTest.CourseDuplicate;
import com.xlh.security.UserInfoHolder;
import com.xlh.service.check.CheckService;
import com.xlh.service.courseTest.CourseDuplicateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author zhr 2019/4/3
 */
@Api(tags = "CheckController", description = "批阅")
@RestController
@RequestMapping("/course/{courseId}/check")
public class CheckController {
    @Autowired
    private CheckService service;
    @Autowired
    private CourseDuplicateService duplicateService;
    @Autowired
    private UserInfoHolder userInfoHolder;

    @ApiOperation("批阅随堂练习显示")
    @PostMapping("")
    @PreAuthorize("@permissionValidator.isTeacher()")
    public Result<List<ClassDTO>> listClass(
            @PathVariable("courseId") Long courseId,
            @RequestBody @Valid CheckClassParam param
    ) {
        return Result.success(service.listClass(param.getClassIdList(), param.getType(), courseId, userInfoHolder.getUserId()));
    }

    @ApiOperation("批阅打包归档：检查归档文件是否存在")
    @PostMapping("/archive-check")
    @PreAuthorize("@permissionValidator.isTeacher()")
    public Result archiveCheck(
            @PathVariable("courseId") Long courseId,
            @RequestBody @Valid CheckClassParam param
    ) {
        if (param.getClassIdList().size() > 1) {
            throw new GlobalException("归档导出操作必须选择一个班级进行");
        }

        service.archiveCheck(courseId, param);

        return Result.success();
    }

    @ApiOperation("批阅打包归档：下载归档文件")
    @PostMapping("/archive")
    @PreAuthorize("@permissionValidator.isTeacher()")
    public Result<PageDTO<ClassDTO>> archive(
            @PathVariable("courseId") Long courseId,
            @RequestBody @Valid CheckClassParam param
    ) {
        if (param.getClassIdList().size() > 1) {
            throw new GlobalException("归档导出操作必须选择一个班级进行");
        }

        service.archive(courseId, param);

        return Result.success();
    }

    @ApiOperation("批阅随堂练习学生列表")
    @GetMapping("/type/{type}/class/{classId}/chapter/{chapterId}")
    @PreAuthorize("@permissionValidator.isTeacher()")
    public Result<PageDTO<StudentListDTO>> listStudent(
            @PathVariable("type") Integer type,
            @PathVariable("classId") Long classId,
            @PathVariable("chapterId") Long chapterId,
            @RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "studentInfo", required = false) String studentInfo
    ) {
        return Result.success(service.listStudent(pageNum, pageSize, type, classId, chapterId, status, studentInfo, userInfoHolder.getUserId()));
    }

    @ApiOperation("满分设置")
    @PostMapping("settingfullmark")
    @PreAuthorize("@permissionValidator.isTeacher()")
    public Result settingfullmark(@RequestBody CheckFullMarkParam checkFullMarkParam) {
        service.settingFullMark(checkFullMarkParam.getClassId(),
                checkFullMarkParam.getCourseId(),  checkFullMarkParam.getChapterId(),
                checkFullMarkParam.getType(), checkFullMarkParam.getScore());
        return Result.success();
    }

    @ApiOperation("满分获取")
    @GetMapping("getFullMark")
    @ApiImplicitParam(name = "type",value = "1 随堂练习  2 实验报告")
    @PreAuthorize("@permissionValidator.isTeacher()")
    public Result getFullMark(@RequestParam("classId") Long classId,
                              @RequestParam("courseId") Long courseId,
                              @RequestParam("chapterId") Long chapterId,
                              @RequestParam("type") Integer type){
        Integer score = service.getFullMark(classId, courseId, chapterId, type);
        return Result.success(score);
    }


    @ApiOperation("批阅详情")
    @GetMapping("/type/{type}/class/{classId}/chapter/{chapterId}/student/{id}")
    @PreAuthorize("@permissionValidator.isTeacher()")
    public Result<CheckDTO> readDetails(
            @PathVariable("type") Integer type,
            @PathVariable("id") Long id,
            @PathVariable("courseId") Long courseId,
            @PathVariable("chapterId") Long chapterId,
            @PathVariable("classId") Long classId) {
        return Result.success(service.readDetails(type, id, chapterId, classId, courseId));
    }
    @ApiOperation("批阅纠正")
    @PostMapping("/jz")
    @PreAuthorize("@permissionValidator.isTeacher()")
    public Result jz(
            @PathVariable("courseId") Long courseId, @RequestParam("jz") boolean jz, @RequestParam("id") Long id, @RequestParam("quitId") Long quitId) {
        service.updateJz(id,jz,quitId);
        return Result.success();
    }
    @ApiOperation("提交批阅结果")
    @PostMapping("/type/{type}/class/{classId}/chapter/{chapterId}/student/{id}")
    @PreAuthorize("@permissionValidator.isTeacher()")
    @PreAcquireLock(value = ReportConstant.UPDATE_PAPER_CHECK)
    public Result<CheckDTO> submitReadPractice(
            @PathVariable("id") Long id, @RequestBody @Valid CheckSubmitParam param, @PathVariable("type") Integer type
    ) {
        service.read(id, param, type);
        return Result.success();
    }

    @ApiOperation("课程查重率设置")
    @GetMapping("/duplicate-percent")
    @PreAuthorize("@permissionValidator.isTeacher()")
    public Result<CourseDuplicate> getDuplicate(@PathVariable("courseId") Long courseId) {
        return Result.success(duplicateService.getDuplicate(courseId));
    }

    @ApiOperation("修改课程查重率设置")
    @PostMapping("/duplicate-percent")
    @PreAuthorize("@permissionValidator.isTeacher()")
    public Result setDuplicate(@PathVariable("courseId") Long courseId, @RequestBody @Valid DuplicateParam param) {
        duplicateService.setDuplicate(courseId, param);
        return Result.success();
    }
    @ApiOperation("重新作答-随堂练习")
    @DeleteMapping("/deletePractice")
    @PreAuthorize("@permissionValidator.isTeacher()")
    public Result deletePractice(@RequestParam("id") Long id, @PathVariable("courseId") Long courseId) {
        service.deletePractice(id);
        return Result.success();
    }


    @ApiOperation("重新作答-实验报告")
    @DeleteMapping("/deleteReport")
    @PreAuthorize("@permissionValidator.isTeacher()")
    public Result deleteReport(@RequestParam("id") Long id) {
        service.deleteReport(id);
        return Result.success();
    }






}
